﻿using DevExpress.Web.ASPxFileManager;
using RR;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Default2 : System.Web.UI.Page
{
    const string UploadDirectory = "/scans/";
    protected RegistRar regrar = new RegistRar();
    string filePath = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //SetEditorsInitialState();
            //UpdateLinkedProperties();
            int code = Convert.ToInt32(Request.QueryString["dscod"]);
            GetDocInfo(code);
            SetPermision();
        }
        else
        {

            // UpdateLinkedProperties();
        }

        ApplyFileManagerSettings();
    }

    private void SetPermision()
    {
        int role = Profile.UserInfo.ActiveRole;
        switch (role)
        {
            case 2:
                fManager.SettingsPermissions.Role = "Заказчик";
                break;
            default:
                fManager.SettingsPermissions.Role = "Оператор";
                break;
        }
    }

    void ApplyFileManagerSettings()
    {
        if (Request.QueryString["dscod"] != null)
        {
            filePath = String.Format("~{0}{1}/", UploadDirectory, Request.QueryString["dscod"]);
            if (Directory.Exists(Page.MapPath(filePath)) == false)
            {
                Directory.CreateDirectory(Page.MapPath(filePath));
            }
            fManager.Settings.RootFolder = filePath;
        }
        else
        {
            fManager.Settings.RootFolder = "~/noscans";
            fManager.Enabled = false;
        }
    }

    protected void GetDocInfo(int code)
    {
        if (code != null)
        {
            var query = (from d in regrar.DOGDOC
                         where d.CODE == code
                         select d).First();

            lbl_dog.Text = String.Format("{0}", query.DOGDOP.DOG1.NOMDOG);
            lbl_dops.Text = String.Format("{0}", query.DOGDOP.DOPNUM);
            lbl_doc.Text = String.Format("{0}", query.NOMER);
            lbl_kontr.Text = String.Format("{0}", query.DOGDOP.DOG1.KONTRAGENT1.FULLNAME);
        }
    }
}